//
//  MyTeamController.m
//  MY Project For Sport
//
//  Created by dllo on 16/2/25.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "MyTeamController.h"
#import "NetworkingHandlerOfBlock.h"
#import "ModelForMyTeam.h"
#import "CellForMyTeam.h"
#import "WebviewForNews.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface MyTeamController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *tableView;


@end

@implementation MyTeamController

- (void)dealloc {
    [_array release];
    [_tableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   // NSLog(@"%@", self.url);
    [self handleData];
    [self createTableView];
    
}

- (void)handleData {

    self.array = [NSMutableArray array];
    
  //  NSLog(@"%@", self.url);
    
[NetworkingHandler networkingWithUrl:self.url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
   
    
    NSDictionary *dic = [result objectForKey:@"result"];
    NSDictionary *dict = [dic objectForKey:@"data"];
    NSDictionary *dicd = [dict objectForKey:@"feed"];
    NSArray *array = [dicd objectForKey:@"data"];
    for (NSDictionary *dicdd in array) {
        
        ModelForMyTeam *model = [[ModelForMyTeam alloc] init];
        
        [model setValuesForKeysWithDictionary:dicdd];
       // NSLog(@"%@", model.title);
        [self.array addObject:model];
        [model release];
        
    }
 
    [self.tableView reloadData];
    
}];
    
}

- (void)createTableView {

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellForMyTeam class] forCellReuseIdentifier:@"poolForMyTeam"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;

}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellForMyTeam *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForMyTeam"];
    
    ModelForMyTeam *model = [self.array objectAtIndex:indexPath.row];
    
    [cell passModelForMyTeam:model];

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    WebviewForNews *web = [[WebviewForNews alloc] init];
    
    ModelForMyTeam *model = [self.array objectAtIndex:indexPath.row];
    
    web.url = model.url;
    
    [self.navigationController pushViewController:web animated:YES];
    
    [web release];


}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return self.view.frame.size.height / 3;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
